package testing;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import dse.Cluster;
import dse.DataObject;
import dse.Network;
import dse.Node;
import dse.Pair;
import dse.modules.ThresholdCacheDecisionMaker;

public class OneCacheDecisionMaker extends ThresholdCacheDecisionMaker {
	
	private DataObject dobj; 
	
	private boolean tookFirst = false;
	private Pair<Cluster,DataObject> requestToMake = null;
	private Cluster cacheCluster; 
	
	public Pair<Cluster,DataObject> getOnlyRequestToMake (Network network)
	{
		if (requestToMake != null)
			return requestToMake;

		assert dobj != null;
		
		requestToMake = new Pair<Cluster,DataObject>(
				getCacheCluster(network), dobj
				);
		return requestToMake;
	}
	
	public Map<Cluster, List<DataObject>> makeCachingDecisions (
			Network network, 
			Map<Node, DataObject> generatedRequests
			)
	{
		if (requestToMake == null) 
			getOnlyRequestToMake(network);
		if (tookFirst)
			return new Hashtable<Cluster, List<DataObject>>();
		
		tookFirst = true;
		Map<Cluster,List<DataObject>> onlyOne = 
			new Hashtable<Cluster,List<DataObject>> ();
		Vector<DataObject> v = new Vector<DataObject> ();
		v.add(requestToMake.second);
		onlyOne.put(requestToMake.first, v);
		return onlyOne;
	}
	
	public Cluster getCacheCluster (Network network) {
		if (cacheCluster == null)
			cacheCluster = network.clusters().iterator().next();
		return cacheCluster;
	}
	
	public void setDataObject (DataObject dobj) {
		this.dobj = dobj;
	}
	
}
